package cn.edu.qlnu.xky.test.dao;

import cn.edu.qlnu.xky.test.common.MsgExchage;
import cn.edu.qlnu.xky.test.common.Page;
import cn.edu.qlnu.xky.test.model.Borrow;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-06-20
 */


public class BorrowDao {

	BaseDao dao = null;

	public BorrowDao() {
		dao = new BaseDao();
	}

	// 获取所有未归还的借阅记录
	public List<Borrow> getUnreturnedBorrows() {
		if (dao.isClosed()) {
			dao = new BaseDao();
		}
		List<Borrow> borrows = new ArrayList<>();
		String sql = "SELECT * FROM t_borrow WHERE isReturn=0 and dueDate < CURDATE()";
		borrows = this.searchBySql(sql);

		return borrows;
	}

	public int countSelfBorrowNum(int userId) {
		String sql = "select count(*) from t_borrow where isReturn=0 and userId=" + userId;
		System.out.printf("countSelfBorrowNum sql=" + sql);
		return new BaseDao().searchCount(sql);
	}

	public int countIsExceed(int userId) {
		String sql = "select count(*) from t_borrow where isExceed=1 and userId=" + userId;
		System.out.printf("countIsExceed sql=" + sql);
		return new BaseDao().searchCount(sql);
	}

	public int borrowBook(Borrow borrow, int maxBorrow) {
		if (countIsExceed(borrow.getUserId())>0 || countSelfBorrowNum(borrow.getUserId())> maxBorrow){
			return 0;
		}
		String sql = "insert INTO t_borrow(userId,bookId,borrowDate,dueDate)"
				+ "values("
				+borrow.getUserId()+","
				+borrow.getBookId()+",'"
				+ MsgExchage.chDate(borrow.getBorrowDate())+"','"
				+MsgExchage.chDate(borrow.getDueDate())+"')";
		System.out.printf("borrowBook sql=" + sql);
		return dao.doAddEditDelete(sql);
	}

	public int updateBorrow(Borrow b) {
		if (dao==null || dao.isClosed()) {
			dao = new BaseDao();
		}
		String sql = "update t_borrow SET "
				+ "dueDate='"+MsgExchage.chDate(b.getDueDate())+"',"
				+ "returnDate="+ (Objects.isNull(b.getReturnDate()) ? "null" : "'"+MsgExchage.chDate(b.getReturnDate())+"'")+","
				+ "isExceed="+b.getIsExceed()+", "
				+ "fine="+b.getFine()+", "
				+ "isReturn="+b.getIsReturn()+" "
				+ "where id="+b.getId();
		return dao.doAddEditDelete(sql);
	}

	public Borrow searchById(int id) {
		String sql = "select * from t_borrow where id="+id;
		List<Borrow> bs = searchBySql(sql);
		if(bs.size() == 0)
			return new Borrow();
		else
			return bs.get(0);
	}

	public Page<Borrow> searchByStatusAndUserId(String userName, Integer adminStatus, Integer isReturn, Integer userId, int pageNo) {
		String whereClase =  " where 1=1 ";
		if (Objects.nonNull(userId)) {
			whereClase += " and tbw.userId="+userId;
		}
		if (Objects.nonNull(userName)) {
			whereClase += " and tu.userName like '%"+userName+"%'";
		}
		if (Objects.nonNull(adminStatus)) {
			switch (adminStatus) {
				case 0:
					whereClase += " and tbw.isExceed=0 and tbw.isReturn=0 ";
					break;
				case 1:
					whereClase += "  and tbw.isExceed=1 and tbw.isReturn=0  ";
					break;
				case 2:
					whereClase += " and tbw.isReturn=1 ";
					break;
				default:
					break;
			}
		}
		if (Objects.nonNull(isReturn)) {
			whereClase += " and tbw.isReturn=" + isReturn;
		}
		whereClase += " order by tu.userName desc";
		String sql = "SELECT tbw.id as id, tbw.userId as userId, tu.userName as userName, tbw.bookId as bookId, " +
				"tb.bookName as bookName, tbw.borrowDate as borrowDate, tbw.dueDate as dueDate, " +
				"tbw.returnDate as returnDate, tbw.isReturn as isReturn, tbw.isExceed as isExceed, " +
				"tbw.fine as fine from t_borrow tbw left join t_user tu on tu.id=tbw.userId " +
				"left join t_book tb on tb.id=tbw.bookId";
		sql += whereClase;
		Page<Borrow> p = new Page<Borrow>();
		p.setPageNo(pageNo);
		List<Map<String,Object>> objs = dao.searchByPage(sql,pageNo);
		int count = new BaseDao().searchCount("select count(*) from t_borrow tbw left join t_user tu on tu.id=tbw.userId left join t_book tb on tb.id=tbw.bookId "+whereClase);
		p.setTotalCount(count);
		List<Borrow> bs = new ArrayList<Borrow>();
		Borrow borrow = null;
		for(Map<String,Object> obj : objs) {
			borrow = new Borrow();
			borrow.setUserId((int)obj.get("userId"));
			borrow.setUserName((String)obj.get("userName"));
			borrow.setBookId((int)obj.get("bookId"));
			borrow.setBookName((String)obj.get("bookName"));
			borrow.setBorrowDate((Date)obj.get("borrowDate"));
			borrow.setDueDate((Date)obj.get("dueDate"));
			borrow.setReturnDate((Date)obj.get("returnDate"));
			borrow.setIsExceed((int)obj.get("isExceed"));
			borrow.setId((int)obj.get("id"));
			borrow.setIsReturn((int)obj.get("isReturn"));
			borrow.setFine((int)obj.get("fine"));
			if (borrow.getIsExceed() == 1) {
				borrow.setOverDueDays();
			}
			bs.add(borrow);
		}
		p.setRecords(bs);
		return p;
	}

	private List<Borrow> searchBySql(String sql){
		List<Borrow> borrows = new ArrayList<Borrow>();
		List<Map<String,Object>> objs = dao.searchBySql(sql);
		Borrow borrow = null;
		for(Map<String,Object> obj : objs) {
			borrow = new Borrow();
			borrow.setUserId((int)obj.get("userId"));
			if (Objects.nonNull(obj.get("userName"))) {
				borrow.setUserName((String)obj.get("userName"));
			}
			borrow.setBookId((int)obj.get("bookId"));
			if (Objects.nonNull(obj.get("bookName"))) {
				borrow.setBookName((String)obj.get("bookName"));
			}
			borrow.setBorrowDate((Date)obj.get("borrowDate"));
			borrow.setDueDate((Date)obj.get("dueDate"));
			borrow.setReturnDate((Date)obj.get("returnDate"));
			borrow.setIsExceed((int)obj.get("isExceed"));
			borrow.setId((int)obj.get("id"));
			borrow.setIsReturn((int)obj.get("isReturn"));
			borrow.setFine((int)obj.get("fine"));
			if (borrow.getIsExceed() == 1) {
				borrow.setOverDueDays();
			}
			borrows.add(borrow);
		}
		return borrows;
	}

}
